package Page.TeachingInfoPage;

import Dao.CourseInfoDao;
import DataClass.Course;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class TeachingInfoController implements Initializable {
    @FXML
    public TextField InputKeyWord;
    @FXML
    public Button FindButton;
    @FXML
    public Button ClearButton;
    @FXML
    private Button BackButton;
    @FXML
    public Button RefreshButton;
    @FXML
    private TableView<Course> TeachingInfoTable;

    @FXML
    private TableColumn<Course,String> Cid;
    @FXML
    private TableColumn<Course,String> Cname;
    @FXML
    private TableColumn<Course,String> Ctype;
    @FXML
    private TableColumn<Course,String> Cstudent;
    @FXML
    private TableColumn<Course,String> Ctime;
    @FXML
    private TableColumn<Course,String> Cteacher;

    // viewModel
    private TeachingInfoViewModel viewModel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        viewModel = new TeachingInfoViewModel();

        Cid.setCellValueFactory(new PropertyValueFactory<>("Cid"));
        Cname.setCellValueFactory(new PropertyValueFactory<>("Cname"));
        Ctype.setCellValueFactory(new PropertyValueFactory<>("Ctype"));
        Cstudent.setCellValueFactory(new PropertyValueFactory<>("Cstudent"));
        Ctime.setCellValueFactory(new PropertyValueFactory<>("Ctime"));
        Cteacher.setCellValueFactory(new PropertyValueFactory<>("Cteacher"));

        TeachingInfoTable.setItems(viewModel.getDataListProperty());

        InputKeyWord.textProperty().bindBidirectional(viewModel.getInputKeyWord());
        InputKeyWord.textProperty().addListener((observable, oldValue, newValue) -> {
            viewModel.updateInputKeyWord(newValue);
        });

        try {
            // 从数据库或其他来源获取数据
            viewModel.setDataList(CourseInfoDao.getCourseInfo());

        } catch (SQLException e) {
            viewModel.setDataList(new ArrayList<>());
            System.err.println("TeachingInfo 数据获取失败 in TeachingInfoController");
        }
    }

    public void backButtonClicked(javafx.event.ActionEvent actionEvent) {
        viewModel.backButtonClicked();
    }

    public void findButtonClicked(javafx.event.ActionEvent actionEvent) {
        viewModel.clickFindButton();
    }

    public void refreshButtonClicked(javafx.event.ActionEvent actionEvent) {
        viewModel.refresh();
        try {
            // 从数据库或其他来源获取数据
            viewModel.setDataList(CourseInfoDao.getCourseInfo());
        } catch (SQLException e) {
            viewModel.setDataList(new ArrayList<>());
            System.err.println("BookBorrowingRecord 数据获取失败" + e.getMessage());
        }
    }

    public void clearButtonClicked(ActionEvent actionEvent) {
        viewModel.clearFilter();
    }
}
